
package com.ht.action;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;
import com.ht.model.User;
import com.ht.service.UserService;
import com.opensymphony.xwork2.ActionSupport;


@SuppressWarnings("serial")
public class UserAction  extends ActionSupport {
	
	User user ;
	
	@Autowired
	private UserService userServiceImpl;
	
	public User getUser() {
		return user;
	}


	public void setUser(User user) {
		this.user = user;
	}

	
	public String userHandler(){
		HttpServletRequest request  = ServletActionContext.getRequest();
		if("0".equals(request.getParameter("act"))){
			request.getSession().setAttribute("username", user.getUsername());
			boolean userExcits = userServiceImpl.userExists(user);
		  	if(userExcits){
		  		String menuStr = " :null,"
		                +"寻找倒霉蛋:null;选倒霉蛋:personAction!select.action;历史记录:historyAction!select.action;模拟抽奖:/test/test/moni.jsp,"
		                +"系统管理员:null;账号管理:manageAction!select.action;成员管理:empAction!select.action,"
		                +" :null";
			    request.getSession().setAttribute("menuStr", menuStr);
		  		return "main";
		  	}else{
		  		request.setAttribute("message", "用户名或密码错误");
		  		return "input";
		  	}
		}else if("1".equals(request.getParameter("act"))){
			userServiceImpl.insertUser(user);
			return "input";
		}else{
			return "input";
		}
		
		
	}
	
	
	
	/*
	public String userHandler(){
		HttpServletRequest request  = ServletActionContext.getRequest();
		if("0".equals(request.getParameter("act"))){
			request.getSession().setAttribute("username", user.getUsername());
			List<User> users = userServiceImpl.findUserByExample(user);
		  	if(user.size() != 0){
				User loginUser = users.getItem(0);
				
				if(loginUser.getPermission() == 0){
					String menuStr = " :null,"
						+"寻找倒霉蛋:null;选取成员 ersonAction!select.action;配置权重 ersonAction!select.action;历史记录 ersonAction!select.action," 
						+" :null"; 
				}else{
					String menuStr = " :null,"
		                +"寻找倒霉蛋:null;选取成员:personAction!select.action;配置权重:personAction!select.action;历史记录:personAction!select.action,"
		                +"系统管理员:null;账号管理:manageAction!select.action;成员管理:empAction!select.action,"
		                +" :null";
				}
			    request.getSession().setAttribute("menuStr", menuStr);
		  		return "main";
		  	}else{
		  		request.setAttribute("message", "用户名或密码错误");
		  		return "input";
		  	}
		}else if("1".equals(request.getParameter("act"))){
			userServiceImpl.insertUser(user);
			return "input";
		}else{
			return "input";
		}

		
	}
	
	*/
	
}
